package com.example.administrator.videotest2.util.Fragment;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;

import com.example.administrator.videotest2.R;
import com.example.administrator.videotest2.Vdetail_Activity;
import com.example.administrator.videotest2.config.VideoApi;
import com.example.administrator.videotest2.entity.VideoCover;
import com.example.administrator.videotest2.util.HttpUtil;
import com.example.administrator.videotest2.util.JsonUtil;
import com.example.administrator.videotest2.util.ResponseJson;
import com.example.administrator.videotest2.util.Tab.VideoAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Tab3_Fragment extends Fragment {


    private View view;
    private PullToRefreshGridView gridView;
    private GridView gridView1;
    private ProgressDialog progressDialog;
    private List<VideoCover> video_list = new ArrayList<>();
    private boolean b;//判断上拉还是下拉
    int index = 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.tab3_fragment, container, false);
        gridView = view.findViewById(R.id.list_view);
        listen(gridView);
        task();
        return view;
    }

    //支持刷新
    private void listen(PullToRefreshGridView gridView) {
        gridView.setMode(PullToRefreshBase.Mode.PULL_UP_TO_REFRESH);
        //设置拉动监听器
        gridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> pullToRefreshBase) {
                b = true;
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> pullToRefreshBase) {
                b = false;
                index++;
                task();
            }
        });
    }


    private void task() {

        Task Mytask = new Task();
        Mytask.execute(VideoApi.dm_url + index);
    }


    class Task extends AsyncTask<String, Void, String> {
        VideoAdapter videoAdapter;
        @Override
        protected String doInBackground(String... strings) {

            OkHttpClient okHttpClient = new OkHttpClient();
            Request request = new Request.Builder().url(strings[0]).build();
            try {
                Response response = okHttpClient.newCall(request).execute();
                if (response.code() == 200) {
                    String s = response.body().string();
                    return s;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Gson gson = new Gson();

            List<VideoCover> list = gson.fromJson(s, new TypeToken<List<VideoCover>>() {
            }.getType());
            if(b){
                gridView.onRefreshComplete();
            }
            //存放数据
            if(list!=null) {
                video_list.addAll(list);
                videoAdapter = new VideoAdapter(getActivity(), R.layout.video_item, video_list);
                gridView.getRefreshableView().setSelection(gridView.getRefreshableView().getCount());
                videoAdapter.notifyDataSetChanged();
                gridView.onRefreshComplete();
                gridView.setAdapter(videoAdapter);
                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        VideoCover cover = (VideoCover) parent.getItemAtPosition(position);
                        Intent intent = new Intent(getActivity(), Vdetail_Activity.class);
                        intent.putExtra("image", cover.getImage().toString());
                        intent.putExtra("str", VideoApi.find_video_img);
                        startActivity(intent);

                    }
                });
            }

        }
    }

}